/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "HuaweiPush.h"
#include "CordovaViewController.h"
REGISTER_PLUGIN_CLASS(HuaweiPush)
bool HuaweiPush::execute(const string&  action, cJSON* args, CallbackContext cbc)
{
    if(action == "register") {
        m_cbc = cbc;
        executeArkTs("/pages/GetToken", 0, "", "HuaweiPushPlugin", cbc);
        pushInfoBeforeInit();
        return true;
    }
    
    if(action == "subscribe") {
        m_cbc = cbc;
        cbc.success("success");
    }
    
    if(action == "unsubscribe") {
        m_cbc = cbc;
        cbc.success("success");
    }
    if(action == "analytics") {
        m_cbc = cbc;
        cbc.success("success");
    }
    
    if(action == "onArKTsResult") {
        return onArKTsResult(args);
    }
    return true;
}

void HuaweiPush::pushInfoBeforeInit() 
{
    if(!Application::g_strPushInfoBeforeInit.empty()) {
        cJSON* pAllJson = cJSON_Parse(Application::g_strPushInfoBeforeInit.c_str());
        if(cJSON_GetObjectItem(pAllJson, "result")) {  //启动前保存的推送，包含json的所有数据因为缓存全局变量中了，没有调用插件，启动后直接调用了插件
            cJSON* json = cJSON_GetObjectItem(pAllJson, "result");
            if(json != NULL && json->type == cJSON_Array) {
                int count = cJSON_GetArraySize(json);
                string type;
                if(count > 0) {
                    type = cJSON_GetArrayItem(json, 0)->valuestring;
                }
                string data;    
                if(count > 0) {
                    data = cJSON_GetArrayItem(json, 1)->valuestring;
                }
                string jsExec = "cordova.fireDocumentEvent('huaweiMessageReceived',";
                jsExec += data + ")";
                ((CordovaViewController*)Application::g_cordovaViewController)->addJavaScript(m_cbc.getWebTag(), jsExec);
            }
        }        
        Application::g_strPushInfoBeforeInit = "";
    }
}
bool HuaweiPush::onArKTsResult(cJSON* args)
{
    string content = cJSON_GetObjectItem(args, "content")->valuestring;
    cJSON* json = cJSON_GetObjectItem(args, "result");
    if(content == "getToken") {
        if(json != NULL && json->type == cJSON_Array) {
            int count = cJSON_GetArraySize(json);
            if(count > 0) {
                string result = cJSON_GetArrayItem(json, 0)->valuestring;
                PluginResult pluginResult(PluginResult::Status::OK, result);
                pluginResult.setKeepCallback(true);
                m_cbc.sendPluginResult(pluginResult);
            } else {
                m_cbc.error("please manually open it in the notification");
            }
        }
    }
    
    if(content == "receiveMessage") {
        if(json != NULL && json->type == cJSON_Array) {
            int count = cJSON_GetArraySize(json);
            string type;
            if(count > 0) {
                type = cJSON_GetArrayItem(json, 0)->valuestring;
            }
            string data;    
            if(count > 0) {
                data = cJSON_GetArrayItem(json, 1)->valuestring;
            }
            string jsExec = "cordova.fireDocumentEvent('huaweiMessageReceived',";
            jsExec += data + ")";
            ((CordovaViewController*)Application::g_cordovaViewController)->addJavaScript(m_cbc.getWebTag(), jsExec);
        }
    }
    return true;
}